package com.david.shortcuts;

import static com.david.shortcuts.ShortCutsProxyService.SHORT_CUTS_ALIPAY_QR;
import static com.david.shortcuts.ShortCutsProxyService.SHORT_CUTS_ALISCAN;
import static com.david.shortcuts.ShortCutsProxyService.SHORT_CUTS_CITY_TRAFFIC;
import static com.david.shortcuts.ShortCutsProxyService.SHORT_CUTS_KEY;
import static com.david.shortcuts.ShortCutsProxyService.SHORT_CUTS_WECHAT_SCAN;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;


public class ShortCutsWidget extends AppWidgetProvider {
    private static final String TAG = "WIDGET";

    public static final String REFRESH_WIDGET = "com.oitsme.REFRESH_WIDGET";
    public static final String PANEL_VIEW_CLICK = "com.oitsme.PANEL_VIEW_CLICK";
    public static final String COLLECTION_VIEW_ACTION = "com.oitsme.COLLECTION_VIEW_ACTION";
    public static final String COLLECTION_VIEW_EXTRA = "com.oitsme.COLLECTION_VIEW_EXTRA";

    /**
     * 桌面小部件更新回调方法，设置view的UI及事件；通过playService的updateWidget触发系统开始更新桌面部件
     */
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                         int[] appWidgetIds) {

        for (int appWidgetId : appWidgetIds) {
            RemoteViews remoteViews = getWidgetView(context);

            if (remoteViews == null) {
                return;
            }
//            // 调用集合管理器对集合进行更新
            appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }


    //    static RemoteViews remoteViews;
    public static RemoteViews getWidgetView(Context context) {
        // 获取AppWidget对应的视图
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.short_cuts_widget);

        // TODO: 2023/9/14 直接使用路由跳转，替换service代理跳转
        /**8.0后用getForegroundService才能在app被kill后直接启动service*/
        Intent aliScanIntent = new Intent(context, ShortCutsProxyService.class);
//        Intent aliScanIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("alipays://platformapi/startapp?saId=10000007"));
        aliScanIntent.setAction(PANEL_VIEW_CLICK);
        aliScanIntent.putExtra(SHORT_CUTS_KEY, SHORT_CUTS_ALISCAN);
        //打开activity时用PendingIntent.getActivity, 启动service时用getService,发送广播时使用getBroadcast
        PendingIntent aliscanPendingIntent =
                PendingIntent.getForegroundService(context, 100, aliScanIntent, PendingIntent.FLAG_IMMUTABLE);

        remoteViews.setOnClickPendingIntent(R.id.shortcut_widget_aliscan, aliscanPendingIntent);


        Intent aliPayQr = new Intent(context, ShortCutsProxyService.class);
//        Intent aliPayQr = new Intent(Intent.ACTION_VIEW, Uri.parse("alipays://platformapi/startapp?saId=20000056"));
        aliPayQr.setAction(PANEL_VIEW_CLICK);
        aliPayQr.putExtra(SHORT_CUTS_KEY, SHORT_CUTS_ALIPAY_QR);

        //打开activity时用PendingIntent.getActivity, 启动service时用getService,发送广播时使用getBroadcast
        PendingIntent aliPayQrPendingIntent = PendingIntent.getForegroundService(context, 101, aliPayQr, PendingIntent.FLAG_IMMUTABLE);

        remoteViews.setOnClickPendingIntent(R.id.shortcut_widget_alipay, aliPayQrPendingIntent);


        Intent weChatScan = new Intent(context, ShortCutsProxyService.class);
//        Intent cityTrafficQr = new Intent(Intent.ACTION_VIEW, Uri.parse("smkapp://scanCodeRide2"));
        weChatScan.setAction(PANEL_VIEW_CLICK);
        weChatScan.putExtra(SHORT_CUTS_KEY, SHORT_CUTS_WECHAT_SCAN);
        //打开activity时用PendingIntent.getActivity, 启动service时用getService,发送广播时使用getBroadcast
        PendingIntent weChantScanPendingIntent = PendingIntent.getForegroundService(context, 103, weChatScan, PendingIntent.FLAG_IMMUTABLE);

        remoteViews.setOnClickPendingIntent(R.id.shortcut_widget_wechatscan, weChantScanPendingIntent);


        Intent cityTrafficQr = new Intent(context, ShortCutsProxyService.class);
//        Intent cityTrafficQr = new Intent(Intent.ACTION_VIEW, Uri.parse("smkapp://scanCodeRide2"));
        cityTrafficQr.setAction(PANEL_VIEW_CLICK);
        cityTrafficQr.putExtra(SHORT_CUTS_KEY, SHORT_CUTS_CITY_TRAFFIC);
        //打开activity时用PendingIntent.getActivity, 启动service时用getService,发送广播时使用getBroadcast
        PendingIntent cityTrafficQrPendingIntent = PendingIntent.getForegroundService(context, 102, cityTrafficQr, PendingIntent.FLAG_IMMUTABLE);

        remoteViews.setOnClickPendingIntent(R.id.shortcut_widget_citytraffic, cityTrafficQrPendingIntent);
        return remoteViews;

    }

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        Log.e("shortcuts", "refresh widget");
    }
}
